﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using System.Web;
namespace YouHP.Pages.Group
{
    public class TopicAdd : YouHP.Pages.Group.GroupBasePage
    {
        protected TopicInfo TopicData { get; set; }
        public int MaxFileSize = 1000;
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

         private new void HandleCommon()
        {
            //判断是否登录
            if (UserData.Id <= 0)
            {
                this.AddErrLine("您还没有登录，请点击登录！");
                this.MsgboxUrl = this.GetUrl("/User/Login.aspx");
                return;
            }

           //判断是否有权限
            CanAddTopic();

        }

        private void CanAddTopic()
        {

            if (GroupUserData.Id <= 0 || GroupUserData.Role == Utility.Constants.RoleNoExsit|| GroupUserData.Role == Utility.Constants.RoleInviting
                || GroupUserData.Role == Utility.Constants.RoleIngore || GroupUserData.Role==Utility.Constants.RoleAppling)
            {
                this.AddErrLine("你还不是圈中成员，不能发帖！");
                return;
            }
            if (GroupUserData.Role == Utility.Constants.RoleDeleted)
            {
                this.AddErrLine("你已经被管理员请出该圈,不能发帖！");
                return;
            }
            if (GroupUserData.Role == Utility.Constants.RoleBlacklist)
            {
                this.AddErrLine("你已经被列为黑名单,不能发帖！");
                return;
            }
            
        }

         private void HandlePost()
        {
            

            TopicInfo info = ValidataForm();
            if (info == null) return;
            string attachedFileAddressName = null;
            if (!string.IsNullOrEmpty(info.TopicFile))
            {
                attachedFileAddressName = Utils.SaveRequestTopicFile(UserData.Id, MaxFileSize);
                if (attachedFileAddressName.Equals(""))
                {
                    AddErrLine("上传文件失败");
                    return;

                }
            }
            info.TopicFile = attachedFileAddressName;

            if (TopicBLL.Instance.AddTopic(info))
            {
                //SaveFile();
                this.Redirect("/Group/Group.aspx?gid=" + GroupData.Id);
            }
            else
            {
                this.AddErrLine("发表失败！");
                return;
            } 
        }

         private void HandleGet()
        {
        }

        TopicInfo ValidataForm()
        {
            TopicInfo info = new TopicInfo();
            info.Title = RequestTools.GetFormString("ttitle").Trim();
            info.Body = RequestTools.GetFormString("tbody").Trim();
            info.TopicFile = HttpContext.Current.Request.Files[0].FileName;

            if (info.Title.Length == 0)
            {
                this.AddErrLine("标题不能空！");
                return null;
            }
            if (info.Title.Length > Config.TitleMaxSize)
            {
                this.AddErrLine("标题长度不能超过" + Config.TitleMaxSize + "字符！");
                return null;
            }

            if (info.Body.Length == 0)
            {
                this.AddErrLine("内容不能为空！");
                return null;
            }
            else if (info.Body.Length > Config.TopicMaxSize)
            {
                this.AddErrLine("内容长度不能大于 " + Config.TopicMaxSize + "字符！");
                return null;
            }
            info.Groupid = GroupData.Id;
            info.GroupType = 0;
            info.PostTime = DateTime.Now;
            info.PostUserId = UserData.Id;
            info.PostUserName = UserData.UserName;
            info.UpdateTime = DateTime.Now;
   
            return info;
        }

    }
}